package com.cyys.modules.mobile.wx;

import com.cyys.common.ql.AR;
import com.cyys.common.thirdInterface.wx.WxDecryptDataUtil;
import com.cyys.common.thirdInterface.wx.WxNoun;
import com.cyys.modules.core.controller.BaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/api/wx")
@Api(tags = "微信相关接口")
@Validated
public class WxAction extends BaseAction {

    @PostMapping(value="/getOpenId")
    @ApiOperation(value = "小程序端获取openId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "登录凭证code", required = true, dataType = "String", paramType = "query")
    })
    public AR getOpenId(@NotBlank(message = "code不能为空！")String code) throws Exception{
        return WxNoun.jscode2session(code);
    }

    /**
     * 微信后台校验与解密开放数据
     * @param encryptDataB64 加密数据
     * @param sessionKeyB64 登陆后的sessionKey
     * @param ivB64 加密向量
     * @return
     */
    @PostMapping("/decryptData")
    @ApiOperation(value = "解密数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "encryptDataB64", value = "加密数据", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ivB64", value = "加密向量", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sessionKeyB64", value = "登录状态", required = true, dataType = "String", paramType = "query")
    })
    public AR decryptData(String encryptDataB64, String ivB64,String sessionKeyB64) throws Exception{
        return AR.ok(WxDecryptDataUtil.wxDecrypt(encryptDataB64,sessionKeyB64,ivB64),"加密成功");
    }
}
